﻿using RabbitMQ.Client;
using RabbitMQ.Client.Framing;
using System;
using System.Collections.Generic;
using System.Text;

namespace RabbitMQProducerTester
{
    class OldDelayedMessageSend
    {
        public static void Send(ConnectionFactory factory)
        {
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "MAIN_QUEUE",
                           durable: true,
                           exclusive: false,
                           autoDelete: false,
                           arguments: null);
                    channel.QueueBind("MAIN_QUEUE", "amq.direct", "MAIN_QUEUE");

                    IDictionary<string, object> args = new Dictionary<string, object>();
                    args.Add("x-dead-letter-exchange", "amq.direct");
                    args.Add("x-dead-letter-routing-key", "MAIN_QUEUE");
                    channel.QueueDeclare("DELAY_QUEUE", true, false, false, args);

                    //发送的消息   
                    for (int i = 0; i < 20; i++)
                    {
                        IBasicProperties properties = new BasicProperties();
                        properties.Expiration = "10000";
                        string content = $"Hello Delayed from {DateTime.Now} {i}";
                        //往队列中发出一条消息     这时候要发送的队列不应该是QUEUE_NAME，这样才能进行转发的
                        channel.BasicPublish("", "DELAY_QUEUE", properties,
                           Encoding.UTF8.GetBytes(content));//  SerializationUtils.serialize(user));
                    }
                }
            }
        }
    }
}
